import {managedDeptTree} from "@/api/system/user.js";

export function useManagedDept() {

    const deptOwn = ref({})

    // 如果当前部门为叶子结点，返回自己为结果集；如果当前部门非叶子结点，返回children
    const managedDeptOptions = computed(() => {
        if (!deptOwn.value) return []
        const managed = deptOwn.value.children || []
        return managed.length == 0 ? [deptOwn.value] : managed
    })


    /* get dept tree */
    function getManagedDeptTree() {
        managedDeptTree().then(response => {
            deptOwn.value = response.data
        })
    }

    onMounted(() => {
        getManagedDeptTree()
    })

    return {
        deptOwn,
        managedDeptOptions
    }
}